package com.zjitc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjitc.pojo.Cart;
import com.zjitc.pojo.Order;
import com.zjitc.pojo.OrderItem;
import com.zjitc.pojo.User;
import com.zjitc.service.UserService;
import com.zjitc.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by YZQ on 2018/5/29.
 */
@Controller
public class ShopOrderController {
    @Autowired
    UserService userService;
    private static final Integer count=3;
    //生成订单
    @RequestMapping("/createOrder")
    public ModelAndView createOrder(
            HttpSession session
    ){
        ModelAndView modelAndView=new ModelAndView();
        //获取用户对象
        User user= (User) session.getAttribute("user");
        if (user==null){
            modelAndView.setViewName("/user/login.jsp");
            return modelAndView;
        }
        //生成一个订单对象
        Order order=new Order();
        order.setOid(UUID.randomUUID().toString().replace("-",""));
        order.setAddress(user.getAddress());
        order.setName(user.getName());
        order.setId(user.getId());
        //状态:1 未付款 2 已付款但未发货 3 已发货但未收货 4 收货交易完成
        order.setAddress("浙江温州");
        order.setPhone("123");
        order.setState(1);
        order.setUser(user);
        order.setOrdertime(CommonUtils.dateToString(new Date()));
        //订单根据购物车生成的（从数据库中查出购物车的信息cartlist）
        List<Cart> cartList=userService.findCartItemByUid(order.getId());
        if (cartList==null){
            modelAndView.setViewName("/add.do");
            return modelAndView;
        }
        //根据遍历cartlist购物车条目信息，生成订单条目（订单总价格total）
        List<OrderItem> orderItemList=new ArrayList<>();
        double total=0.0;
        for(Cart c:cartList){
            OrderItem orderItem=new OrderItem();
            orderItem.setOrder(order);
            orderItem.setCar(c.getCar());
            orderItem.setCarid(c.getCar().getCarid());
            orderItem.setOid(order.getOid());
            orderItem.setCount(c.getNum());
            //评价状态：0：无操作 1：去评价 2.评价成功
            orderItem.setOsatate(0);
            orderItem.setSubtotal(c.getCar().getCaroriginalprice()*c.getNum());
            total=total+orderItem.getSubtotal();
            orderItemList.add(orderItem);
        }

        order.setOrderItemList(orderItemList);
        //设置订单总价
        order.setTotal(total);
        /*将订单插入数据库*/

        userService.addOrder(order);
        //将订单条目插入数据库
        for (OrderItem orderItem:orderItemList){
            userService.addOrderItem(orderItem);

        }
        //清空购物车
        userService.clearCart(user.getId());
      //显示导航栏
      modelAndView.addObject("order",order);
      modelAndView.setViewName("/orderAll.do");

        return modelAndView;
    }
  @RequestMapping(value = "/orderAll")
  public ModelAndView orderAll(HttpSession session, @RequestParam(required = true, defaultValue = "1")Integer page){
    ModelAndView modelAndView=new ModelAndView();
    User user= (User) session.getAttribute("user");
    if(user==null){
      modelAndView.addObject("wdl","您还未登陆，请先登陆");
      modelAndView.setViewName("/user/login.jsp");
      return modelAndView;
    }
    PageHelper.startPage(page,count);
    List<Order> ordersList=userService.findOrderAll(user.getId());
    PageInfo<Order> pageInfo = new PageInfo<Order>(ordersList);
    modelAndView.addObject("pg",pageInfo);
    modelAndView.addObject("order",ordersList);
    modelAndView.setViewName("/userCenterUI.do");
    return modelAndView;
  }
  @RequestMapping(value = "/payment")
  public ModelAndView payment(HttpSession session,
      Order orders){
    ModelAndView modelAndView=new ModelAndView();
    User user= (User) session.getAttribute("user");
    if(user==null){
      modelAndView.addObject("wdl","您还未登陆，请先登陆");
      modelAndView.setViewName("/user/login.jsp");
      return modelAndView;
    }
    Order orders1=userService.findOrderByOid(orders.getOid());
    double total=orders1.getTotal();
    String totals=String.valueOf(total);
    modelAndView.addObject("total",totals);
    modelAndView.addObject("oid",orders.getOid());
    modelAndView.setViewName("/alipay/alipay.trade.page.pay.jsp");
    return modelAndView;
  }
    /*
    买家账号:vqfrji0751@sandbox.com
    登录密码:111111
    */
  @RequestMapping(value = "/alter")
  public ModelAndView alter(@RequestParam("out_trade_no") String oid){
    ModelAndView modelAndView=new ModelAndView();
    userService.updateOid(oid);
    modelAndView.setViewName("redirect:/orderAll.do");
    return modelAndView;
  }
}
